//
//  TTMusicSingerViewController.m
//  TTMusic
//
//  Created by chenpengyu on 16/3/30.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "TTMusicSingerViewController.h"
#import "TTMusicSingerModel.h"
#import "CPYSingerCollectionViewCell.h"
#import "CPYSingerDetailViewController.h"
@interface TTMusicSingerViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>


@property (nonatomic, strong) NSMutableArray *dataDetailSource;
@property (nonatomic, strong) UITableView *leftTableView;
@property (nonatomic, strong) UITableView *rightTableView;
@property (nonatomic, assign) NSInteger currentListIndex;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) TTMusicBaseCollectionView *collectionView;
@property (nonatomic, strong) TTMusicSingerModel *singerModel;
@end

@implementation TTMusicSingerViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:YES];
    
}
- (void)creatData {
    [TTMusicAFNetworkTool GETNetWithUrl:@"http://v1.ard.tj.itlily.com/ttpod?id=46" body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
        //        NSLog(@"%@", result);
        for (NSDictionary *dic in result[@"data"]) {
            TTMusicSingerModel *singerModel = [[TTMusicSingerModel alloc] init];
            [singerModel setValuesForKeysWithDictionary:dic];
            [_dataSource addObject:singerModel];
        }
        [_collectionView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
    self.dataSource = [NSMutableArray array];
    self.dataDetailSource = [NSMutableArray array];
    [self creatNavButtonItem];
    [self createCollectionView];
    [self creatData];
    // Do any additional setup after loading the view.
}
- (void)creatNavButtonItem {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]  initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
}
- (void)leftBarButtonAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SysWidth / 3, SysWidth / 3);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    self.collectionView = [[TTMusicBaseCollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
//    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[CPYSingerCollectionViewCell class] forCellWithReuseIdentifier:@"CPYSingerCollectionViewCellIdentifier"];
    [self.view addSubview:_collectionView];
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CPYSingerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CPYSingerCollectionViewCellIdentifier" forIndexPath:indexPath];
    cell.singerModel = _dataSource[indexPath.item];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CPYSingerDetailViewController *singerDetailVC = [[CPYSingerDetailViewController alloc] init];
    TTMusicSingerModel *singerModel = _dataSource[indexPath.item];
    singerDetailVC.detailID = [NSString stringWithFormat:@"%@", singerModel.ID];
    [self.navigationController pushViewController:singerDetailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
